#!/usr/bin/env python
# encoding: utf-8
'''
@author: liuchong
@contact: lucas@007vin.com
@software: garner
@file: banzhou.py
@time: 2020/8/24 2:44 下午
@desc:
'''

import sys
sys.path.append('../../')
import pandas as pd
import ujson
import re

from pymongo import MongoClient

from models.audi.audi_193 import CarsPartNumNew, Vb, CarsSaDesc
from scripts.fenci import remove_tt
vwag_parts = CarsPartNumNew.select(CarsPartNumNew.pid).where(
    (CarsPartNumNew.pid != '') & ((CarsPartNumNew.model != '') | (CarsPartNumNew.sa_code != ''))).distinct().order_by(CarsPartNumNew.pid).limit(50000).offset(50000)

s = ['2缸', '二缸', '3缸', '三缸', '4缸', '四缸', '5缸', '五缸', '6缸', '六缸', '8缸', '八缸', '10缸', '十缸', '12缸', '十二缸']
qd = ['全轮驱动', '前驱动', '前部驱动', '四轮驱动']
bsx = ['7档', '7档自动', '7档手动', '7档双离合', '七档', '七档自动', '七档手动', '七档双离合', '6档', '6档自动', '6档手动', '6挡手自一体',
       '六档', '六档自动', '六档手动', '5档', '5档自动', '5档手动', '五档', '五档自动', '五档手动', '5挡手自一体',
       '4档', '4档自动', '4档手动', '四档', '四档自动', '四档手动', '8档', '8档自动', '8档手动', '8挡手自一体',
       '八档', '八档自动', '八档手动', '3档', '3档自动', '3档手动', '三档', '三档自动', '三档手动', '无级自动']
pl = ['1.4', '1.8', '2.0', '2.23', '1.9', '3.0', '1.6', '2.5', '2.8', '2.2', '2.3', '5.2', '2.4', '2.1', '3.2', '1.3', '1.1', '2.7', '4.0', '3.6', '4.2', '6.0', '2.6', '1.2', '1.5', '6.3', '1.7', '0.8', '3.7', '1.0', '3.3', '2.9', '0.9', '5.0']
fdjdm = ['AAD', 'AAE', 'AAH', 'AAN', 'AAR', 'AAS', 'AAT', 'AAZ', 'ABB', 'ABC', 'ABH', 'ABK', 'ABM', 'ABP', 'ABT', 'ABY', 'ABZ', 'ACE', 'ACK', 'ACZ', 'ADA', 'ADP', 'ADR', 'ADU', 'ADW', 'AEB', 'AEC', 'AEH', 'AEJ', 'AEL', 'AEM', 'AEW', 'AFB', 'AFC', 'AFF', 'AFM', 'AFN', 'AFY', 'AGA', 'AGB', 'AGE', 'AGH', 'AGN', 'AGR', 'AGU', 'AHA', 'AHC', 'AHF', 'AHH', 'AHK', 'AHL', 'AHU', 'AJG', 'AJK', 'AJL', 'AJM', 'AJP', 'AJQ', 'AKB', 'AKC', 'AKE', 'AKF', 'AKG', 'AKH', 'AKJ', 'AKL', 'AKN', 'ALF', 'ALG', 'ALH', 'ALT', 'ALW', 'ALZ', 'AMB', 'AMF', 'AMK', 'AML', 'AMM', 'AMU', 'AMX', 'ANA', 'ANB', 'ANK', 'ANQ', 'ANY', 'ANZ', 'APB', 'APC', 'APF', 'APG', 'APP', 'APR', 'APS', 'APT', 'APU', 'APX', 'APY', 'APZ', 'AQA', 'AQD', 'AQE', 'AQF', 'AQG', 'AQH', 'AQJ', 'ARE', 'ARG', 'ARH', 'ARJ', 'ARK', 'ARM', 'ARN', 'ARS', 'ART', 'ARU', 'ARX', 'ARY', 'ARZ', 'ASB', 'ASE', 'ASG', 'ASJ', 'ASM', 'ASN', 'ASV', 'ASZ', 'ATC', 'ATD', 'ATJ', 'ATL', 'ATQ', 'ATW', 'ATX', 'AUA', 'AUK', 'AUL', 'AUM', 'AUQ', 'AUW', 'AUX', 'AVB', 'AVF', 'AVG', 'AVJ', 'AVK', 'AVP', 'AVU', 'AVV', 'AWA', 'AWL', 'AWM', 'AWN', 'AWP', 'AWT', 'AWX', 'AXR', 'AXW', 'AXX', 'AYM', 'AYS', 'AZA', 'AZB', 'AZC', 'AZR', 'AZV', 'BAD', 'BAG', 'BAH', 'BAM', 'BAR', 'BAS', 'BAT', 'BAU', 'BBD', 'BBG', 'BBJ', 'BBK', 'BBY', 'BCS', 'BCY', 'BCZ', 'BDB', 'BDG', 'BDH', 'BDV', 'BDW', 'BDX', 'BEA', 'BEL', 'BES', 'BEX', 'BFB', 'BFC', 'BFL', 'BFM', 'BFQ', 'BFV', 'BGB', 'BGK', 'BGN', 'BGU', 'BHC', 'BHE', 'BHF', 'BHK', 'BHT', 'BHZ', 'BKB', 'BKC', 'BKD', 'BKE', 'BKH', 'BKN', 'BLB', 'BLF', 'BLP', 'BLR', 'BLS', 'BLX', 'BLY', 'BMB', 'BMC', 'BMJ', 'BMK', 'BMM', 'BMN', 'BNA', 'BND', 'BNG', 'BNK', 'BNS', 'BPF', 'BPG', 'BPJ', 'BPK', 'BPP', 'BPW', 'BPY', 'BRB', 'BRC', 'BRD', 'BRE', 'BRF', 'BRV', 'BSB', 'BSE', 'BSF', 'BSG', 'BSM', 'BTE', 'BTR', 'BUB', 'BUG', 'BUH', 'BUJ', 'BUL', 'BUN', 'BUY', 'BVA', 'BVF', 'BVG', 'BVJ', 'BVN', 'BVP', 'BVR', 'BVY', 'BVZ', 'BWA', 'BWE', 'BWT', 'BXA', 'BXE', 'BYH', 'BYK', 'BYT', 'BYU', 'BZB', 'BZC', 'CABA', 'CABB', 'CABD', 'CADA', 'CAEA', 'CAEB', 'CAED', 'CAGA', 'CAGB', 'CAGC', 'CAHA', 'CAHB', 'CAJA', 'CAKA', 'CALA', 'CALB', 'CAMA', 'CAMB', 'CANA', 'CANB', 'CANC', 'CAND', 'CAPA', 'CASA', 'CASB', 'CATA', 'CAUA', 'CAVG', 'CAWB', 'CAXA', 'CAXC', 'CAYB', 'CAYC', 'CBAA', 'CBAB', 'CBBB', 'CBEA', 'CBFA', 'CBRA', 'CBZA', 'CBZB', 'CCAA', 'CCBA', 'CCDA', 'CCEA', 'CCFA', 'CCFC', 'CCGA', 'CCLA', 'CCMA', 'CCSA', 'CCTA', 'CCUA', 'CCWA', 'CCWB', 'CCZA', 'CCZC', 'CDAA', 'CDHA', 'CDHB', 'CDLA', 'CDLB', 'CDLC', 'CDLH', 'CDMA', 'CDNA', 'CDNB', 'CDNC', 'CDRA', 'CDSB', 'CDTA', 'CDTB', 'CDTC', 'CDUC', 'CDUD', 'CDYA', 'CDYB', 'CDYC', 'CDZA', 'CEJA', 'CEPA', 'CEPB', 'CESA', 'CETA', 'CEUA', 'CEUC', 'CFFA', 'CFFB', 'CFGB', 'CFGC', 'CFGD', 'CFHB', 'CFHD', 'CFKA', 'CFSA', 'CGKA', 'CGKB', 'CGLA', 'CGLB', 'CGLC', 'CGLD', 'CGLE', 'CGMA', 'CGQB', 'CGTA', 'CGWA', 'CGWB', 'CGWC', 'CGWD', 'CGXA', 'CGXB', 'CGXC', 'CHHB', 'CHHC', 'CHJA', 'CHMA', 'CHMB', 'CHPB', 'CHVA', 'CHZB', 'CHZD', 'CHZE', 'CHZJ', 'CJBA', 'CJCA', 'CJCB', 'CJCC', 'CJCD', 'CJEB', 'CJED', 'CJEE', 'CJGA', 'CJGC', 'CJMA', 'CJSA', 'CJSB', 'CJTB', 'CJTC', 'CJWB', 'CJWC', 'CJWE', 'CJXB', 'CJXC', 'CJXD', 'CJXF', 'CJXG', 'CJZA', 'CKVB', 'CKVC', 'CKVD', 'CLAA', 'CLAB', 'CLHA', 'CLJA', 'CLLB', 'CLXA', 'CLXB', 'CLZB', 'CMBA', 'CMDA', 'CMEA', 'CMFA', 'CMFB', 'CMGA', 'CMGB', 'CMHA', 'CMPA', 'CMSA', 'CMUA', 'CMXA', 'CNAA', 'CNCB', 'CNCD', 'CNCE', 'CNDA', 'CNHA', 'CNHC', 'CNRB', 'CNSB', 'CNTC', 'CNVA', 'CNYA', 'CNYB', 'CPAA', 'CPEA', 'CPMA', 'CPMB', 'CPNA', 'CPNB', 'CPSA', 'CPTA', 'CPWA', 'CRBC', 'CRBD', 'CRCA', 'CRDB', 'CREA', 'CREC', 'CRED', 'CREG', 'CREH', 'CRFA', 'CRFC', 'CRHA', 'CRKB', 'CRLB', 'CRLC', 'CRTC', 'CRTD', 'CRTE', 'CRTF', 'CRUA', 'CSPA', 'CSPB', 'CSSA', 'CSUA', 'CSUB', 'CSUD', 'CSUE', 'CSWB', 'CTBA', 'CTBB', 'CTBC', 'CTBD', 'CTCB', 'CTCC', 'CTDA', 'CTDB', 'CTEC', 'CTFA', 'CTGA', 'CTGE', 'CTGF', 'CTHG', 'CTNA', 'CTPA', 'CTPB', 'CTSA', 'CTTA', 'CTUA', 'CTUB', 'CTUC', 'CTUD', 'CTVA', 'CTWA', 'CTWB', 'CTXA', 'CTYA', 'CUEA', 'CUFA', 'CUHA', 'CUHB', 'CUJA', 'CUKB', 'CULB', 'CULC', 'CUNA', 'CUSB', 'CUVB', 'CUVC', 'CUVD', 'CUWA', 'CVBA', 'CVJA', 'CVKB', 'CVLA', 'CVMD', 'CVNA', 'CVPA', 'CVUA', 'CVUB', 'CVUC', 'CVZA', 'CWGD', 'CWLA', 'CWNA', 'CWPB', 'CWUB', 'CWUC', 'CWZA', 'CXMA', 'CXSA', 'CXSB', 'CXUA', 'CXXB', 'CYFB', 'CYGA', 'CYLA', 'CYMC', 'CYNB', 'CYPA', 'CYPB', 'CYRB', 'CYRC', 'CYVB', 'CYYA', 'CZAA', 'CZAC', 'CZCA', 'CZCC', 'CZDA', 'CZDB', 'CZDD', 'CZEA', 'CZGA', 'CZGB', 'CZHA', 'CZJA', 'CZPB', 'CZRA', 'CZSE', 'CZVA', 'CZVB', 'CZVC', 'CZVD', 'CZVE', 'CZVF', 'CZZA', 'CZZB', 'DADA', 'DAJB', 'DAXB', 'DAXC', 'DAYB', 'DAZA', 'DBBA', 'DBFB', 'DBGA', 'DBKA', 'DBPA', 'DBRA', 'DBSA', 'DCPC', 'DCYA', 'DDCA', 'DDCB', 'DDDA', 'DDEA', 'DDTA', 'DDVC', 'DDWA', 'DDYA', 'DECA', 'DEHA', 'DEJA', 'DEJB', 'DEMA', 'DESA', 'DETA', 'DETB', 'DEUA', 'DEUB', 'DEUC', 'DFGA', 'DFHA', 'DFTA', 'DFTB', 'DFTC', 'DFUA', 'DGCA', 'DHDA', 'DJHA', 'DJHB', 'DJJA', 'DJYA', 'DKAA', 'DKAB']
client = MongoClient('192.168.10.170', 27017)
db = client.standard_label
collection = db.standard_label_unique
item = []
flag = 0
for a in vwag_parts:
    # 配置代码
    sa_codes = []
    # 发动机排量
    cylinder = []
    # 发动机代码
    vb = []
    # 发动机排量信息
    vb_desc = []

    # model_desc = []
    transmission = []
    drive = []
    diff_mech = []
    # 匹配信息
    label_info = CarsPartNumNew.get_or_none(CarsPartNumNew.pid == a.pid)
    label = label_info.label
    if label:
        label = re.findall(r'[\u4e00-\u9fa5]', label)
        label = ''.join(label)
    stand_info = collection.find_one({'pid': a.pid, 'brandCode': 'audi'})
    if stand_info:
        stand_label = stand_info['standard_label']
    else:
        stand_label = ''
    # 处理audi_copy表中的label字段
    parts = CarsPartNumNew.select().where(CarsPartNumNew.pid == a.pid)
    for part in parts:
        cid = part.cid
        part_sa_codes = part.sa_code or '{}'
        part_sa_codes = ujson.loads(part_sa_codes)

        part_remark = part.remark or ''
        # 处理audi part表和structure表的sa_code

        for key, value in part_sa_codes.items():
            if value:
                sa_codes = sa_codes+value
        # print(sa_codes)

        #处理structure表和part表的model字段

        part_model = part.model
        part_model = part_model.replace('<br/>', '')

        # 筛选出structure表model字段中的缸数和排量信息
        part_out = remove_tt(part_model)
        part_remark_out = remove_tt(part_remark)

        if part_remark:
            for ind, key in enumerate(s):
                if key in part_remark_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)

            for key in pl:
                if key in part_remark:
                    vb_desc.append(key)
        if part_model:
            for key in bsx:
                if key in part_out:
                    key = key.replace('三', '3')
                    key = key.replace('四', '4')
                    key = key.replace('五', '5')
                    key = key.replace('六', '6')
                    key = key.replace('七', '7')
                    key = key.replace('八', '8')
                    key = key.replace('档', '挡')
                    if key == '5挡':
                        key = '5挡手动'
                    if key == '6挡':
                        key = '6挡手动'

                    if key == '6挡自动':
                        key = '6挡AMT'
                    if key == '7挡自动':
                        key = '7挡双离合'
                    if key == '无级自动':
                        key = 'CVT无级变速'
                    transmission.append(key)
            for key in qd:
                if key in part_out:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            for ind, key in enumerate(s):
                if key in part_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)
            for key in pl:
                if key in part_model:
                    vb_desc.append(key)
            if not part_model.startswith('PR:') and not part_model.startswith('PR-') and not part_model.startswith('pr:') and not part_model.startswith('pr-'):
                for key in fdjdm:
                    if key in part_model:
                        vb.append(key)
            if 'PR:' in part_model or 'PR-' in part_model or 'pr:' in part_model or 'pr-' in part_model:
                # 处理part_model中的PR
                part_pr = re.sub('[\u4e00-\u9fa5]', '', part_model)
                part_pr = part_pr.replace('PR:', '')
                part_pr = part_pr.replace('PR-', '')
                part_pr = part_pr.replace('+', ',')
                part_pr = part_pr.replace('/', ',')
                part_pr = part_pr.replace(':', ',')
                part_pr = part_pr.replace('_', ',')
                part_pr = part_pr.split(',')
                for j in part_pr:
                    sa_codes.append(j)

    sa_codes = set(sa_codes)
    sa_codes = list(sa_codes)

    vb = set(vb)
    vb = list(vb)
    # print(vb)

    # 筛选sa_code对应的描述信息
    for i in sa_codes:
        contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == 'audi')
        if contrast and contrast.desc:
            # model_desc.append(contrast.desc)
            if '发动机' in contrast.desc:
                # 筛选出sa表desc中的排量信息
                for key in pl:
                    if key in contrast.desc:
                        vb_desc.append(key)
                for key in s:
                    if key in contrast.desc:
                        key = key.replace('二缸', '2')
                        key = key.replace('2缸', '2')
                        key = key.replace('三缸', '3')
                        key = key.replace('3缸', '3')
                        key = key.replace('四缸', '4')
                        key = key.replace('4缸', '4')
                        key = key.replace('五缸', '5')
                        key = key.replace('5缸', '5')
                        key = key.replace('六缸', '6')
                        key = key.replace('6缸', '6')
                        key = key.replace('八缸', '8')
                        key = key.replace('8缸', '8')
                        key = key.replace('十缸', '10')
                        key = key.replace('10缸', '10')
                        key = key.replace('十二缸', '12')
                        key = key.replace('12缸', '12')
                        if key not in cylinder:
                            cylinder.append(key)

            # 处理变速器配置
            if ('变速箱' in contrast.desc or '变速器' in contrast.desc) and ('档' in contrast.desc or '无级' in contrast.desc):
                for key in bsx:
                    if key in contrast.desc:
                        key = key.replace('三', '3')
                        key = key.replace('四', '4')
                        key = key.replace('五', '5')
                        key = key.replace('六', '6')
                        key = key.replace('七', '7')
                        key = key.replace('八', '8')
                        key = key.replace('档', '挡')
                        if key == '5挡':
                            key = '5挡手动'

                        if key == '6挡':
                            key = '6挡手动'
                        if key == '6挡自动':
                            key = '6挡AMT'
                        if key == '7挡自动':
                            key = '7挡双离合'
                        if key == '无级自动':
                            key = 'CVT无级变速'
                        transmission.append(key)

            # 处理驱动配置
            for key in qd:
                if key in contrast.desc:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            # 处理差速器配置
            if '差速' in contrast.desc or '减速' in contrast.desc:
                diff_mech.append(i)

    # 筛选model中发动机代码对应的排量信息
    for i in vb:
        # print(Vb.aa.zfill(3))
        # break
        vb_AF = Vb.get_or_none(Vb.ab.contains(i), Vb.brand == 'audi')
        if vb_AF and vb_AF.af:
            vb_desc.append(str(int(vb_AF.af)/100))

    # print(vb)
    vb_desc = set(vb_desc)
    vb_desc = list(vb_desc)
    vb_desc = ','.join(vb_desc)

    cylinder = set(cylinder)
    cylinder = list(cylinder)
    cylinder = ','.join(cylinder)

    transmission = set(transmission)
    transmission = list(transmission)
    transmission = ','.join(transmission)

    drive_info = []
    drive = set(drive)
    drive = list(drive)
    for i in drive:
        if i == '前置前驱':
            drive_info.append('两驱')
        if i == '全轮驱动':
            drive_info.append('四驱')
    drive_new = ','.join(drive_info)
    drive = ','.join(drive)

    diff_mech = set(diff_mech)
    diff_mech = list(diff_mech)
    diff_mech = ','.join(diff_mech)

    vb = ','.join(vb)
    unit = {'pid': a.pid, 'standard_label': stand_label, 'label': label, 'desc': '', 'engine_code': vb,
            'cylinder': cylinder, 'transmission': transmission, 'trans_desc': transmission, 'replace': '',
            'drive': drive, '差速器': diff_mech, 'displacement': vb_desc, 'brandCode': 'audi', 'drive_new': drive_new}
    print(unit)
    item.append(unit)

df = pd.DataFrame(item)
df['pid'] = df['pid'].astype('str')
df.to_csv('./export/audi_all02.csv', mode='a', index=False)


